﻿Public Class AddQuizQuestions
    Inherits System.Web.UI.Page

     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

        
        If (Not Page.IsPostBack) Then
            Dim _contextQuizID As HttpContext = HttpContext.Current
            If (_contextQuizID.Items("QuizId") <> Nothing) Then
                ViewState("QuizId") = Convert.ToInt32(_contextQuizID.Items("QuizId"))
                updateContent()

            Else
                Response.Redirect("~/Teachers/AddQuiz.aspx")
            End If
        End If

        Catch ex As Exception

        End Try


    End Sub

    Private Sub updateContent()
        Try


            Dim numberOfQuestionsSubmitted As Int32 = New Quiz_QuestionManagement().SelectNumberOfQuestionsPerQuiz(Convert.ToInt32(ViewState("QuizId")))
            If (numberOfQuestionsSubmitted = 6) Then
                pnlAddQustionsContent.Visible = False
                pnlQuizSubmitted.Visible = True

            Else
                btnAddQuestion.Text = "Add Question " + Convert.ToString((numberOfQuestionsSubmitted + 1))
                numberOfQuestionsSubmitted = 6 - numberOfQuestionsSubmitted
                lblNumberOfQuestionsLeft.Text = "Number of Questions Left: " + numberOfQuestionsSubmitted.ToString()
            End If

            txtAnswerExplenation.Text = ""
            txtPossibleAnswer1.Text = ""
            txtPossibleAnswer2.Text = ""
            txtPossibleAnswer3.Text = ""
            txtPossibleAnswer4.Text = ""
            txtQuestion.Text = ""
            drpPossibleAnswers.SelectedIndex = 0
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub btnAddQuestionClick(sender As Object, e As EventArgs) Handles btnAddQuestion.Click
        Try


            Dim QuizId As Int32 = Convert.ToInt32(ViewState("QuizId"))
            Dim myGuid As New Guid
            myGuid = Guid.NewGuid()

            Dim fileIsAnImage As Boolean = False
            Dim fileName As String
            Dim myFileName As String = String.Empty

            If (fuPictureUrl.HasFile) Then

                fileName = Server.HtmlEncode(fuPictureUrl.FileName)
                Dim extension As String = System.IO.Path.GetExtension(fileName)

                If (extension = ".jpg") Then
                    myFileName = myGuid.ToString() + ".jpg"
                    fileIsAnImage = True
                ElseIf (extension = ".png") Then
                    myFileName = myGuid.ToString() + ".png"
                    fileIsAnImage = True
                End If


                If (fileIsAnImage) Then
                    Dim filePath = Request.MapPath("../Pictures/Quiz") + "/" + myFileName
                    fuPictureUrl.SaveAs(filePath)

                    Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
                    Dim quizQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                    quizQuestion.InsertQuizQuestion(QuizId, txtQuestion.Text, txtPossibleAnswer1.Text, txtPossibleAnswer2.Text, txtPossibleAnswer3.Text, txtPossibleAnswer4.Text, drpPossibleAnswers.SelectedValue, "Pictures/Quiz/" + myFileName, txtAnswerExplenation.Text)

                    'add ranking
                    Dim myPoints As Users_RankingPointsManagement = New Users_RankingPointsManagement()
                    Dim rankingPointsId As Int32 = myPoints.SelectRankingPointsByType("Add Quiz")

                    Dim aCookie As HttpCookie = Request.Cookies("userIdCookie")
                    Dim myuserId = Convert.ToInt32(aCookie.Value)

                    myPoints.InsertUsersRankingPoints(myuserId, rankingPointsId)


                    lblMessage.Text = "Question Submitted. Points awarded. Add Next Question."
                    updateContent()

                Else
                    lblMessage.Text = "Only jpeg or png files are allowed"
                End If
            Else
                lblMessage.Text = "Image is needed to submit a question"

            End If


        Catch ex As Exception

        End Try
    End Sub
End Class